package com.mxpio.erp.equipment.service.impl;

import cn.hutool.core.util.StrUtil;
import com.mxpio.erp.common.equipment.entity.EquipmentStockCkApplyItem;
import com.mxpio.erp.common.equipment.entity.EquipmentStockRkApplyItem;
import com.mxpio.erp.common.equipment.service.EquipmentStockRkApplyItemService;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: 入库单明细
 * @Author: jeecg-boot
 * @Date:   2021-10-07
 * @Version: V1.0
 */
@Service
public class EquipmentStockRkApplyItemServiceImpl extends BaseServiceImpl<EquipmentStockRkApplyItem> implements EquipmentStockRkApplyItemService {


	@Override
	@Transactional(readOnly = true)
	public List<EquipmentStockRkApplyItem> selectByMainId(String mainId) {
        Criteria c = Criteria.create().addCriterion("applyId", Operator.EQ, mainId);
        return list(EquipmentStockRkApplyItem.class,c);
	}

	@Override
	@Transactional(readOnly = true)
	public Page<EquipmentStockRkApplyItem> queryByMainId(Pageable page, String mainId) {
        Criteria c = Criteria.create().addCriterion("applyId", Operator.EQ, mainId);
        return JpaUtil.linq(EquipmentStockRkApplyItem.class).where(c).paging(page);
	}

	@Override
	@Transactional(readOnly = true)
	public Page<EquipmentStockRkApplyItem> queryRkPostingByMainId(Pageable page, String mainId) {
        String sql = "SELECT rai.id_ as id ,rai.apply_id_ as applyId,rai.batch_no_ as batchNo,rai.materials_id_ as materialsId,rai.real_price_ as realPrice,rai.quantity_ as quantity,rai.amount_ as amount,rai.rk_type_id_ as rkTypeId,rai.status_ as status,rai.vendor_id_ as vendorId, rai.remark_ as remark, rai.version_ as version," +
				" tm.NAME_ AS materialsName, tm.CODE_ AS materialsCode, tm.product_name_ AS materialsType, tm.brand_ AS materialsBrand, tm.spec_ AS materialsSpec, tm.stock_unit_ AS materialsUnit, tm.es_price_ AS materialsEsPrice, IFNULL(tmp.current_accept,0) AS receivedNum " +
				"FROM mb_erp_equipment_stock_rk_apply_item rai " +
				"LEFT JOIN mb_erp_equipment_material tm ON tm.id_ = rai.materials_id_ " +
				"LEFT JOIN ( SELECT rh.inboud_apply_item_id_, sum(rh.current_accept_) AS current_accept FROM mb_erp_equipment_stock_rk_history rh WHERE rh.inbound_apply_id_ = '{}' GROUP BY rh.inboud_apply_item_id_ ) tmp ON rai.id_ = tmp.inboud_apply_item_id_ " +
				"WHERE rai.apply_id_ = '{}' AND rai.status_ = 1";

		sql = StrUtil.format(sql,mainId, mainId);
        //List resultList = JpaUtil.nativeQuery(sql, EquipmentStockRkApplyItem.class).getResultList();
        List resultList = JpaUtil.nativeQuery(sql, "StockRkApplyItemDTOMapping").getResultList();
        String countSql = sql + " limit " + (page.getPageNumber()+1)*page.getPageSize() + ","+page.getOffset();
        List total = JpaUtil.nativeQuery(countSql).getResultList();
        return new PageImpl<EquipmentStockRkApplyItem>(resultList, page, total.size());
	}


	@Override
	@Transactional(readOnly = false)
	public boolean saveItem(EquipmentStockRkApplyItem tpmStockRkApplyItem) {
		tpmStockRkApplyItem.setIsDel(0);
		tpmStockRkApplyItem.setVersion(0);
		tpmStockRkApplyItem.setStatus(0);
		super.save(tpmStockRkApplyItem);
		return true;
	}

	@Override
	@Transactional(readOnly = false)
	public boolean updateItemById(EquipmentStockRkApplyItem tpmStockRkApplyItem) {
		tpmStockRkApplyItem.setVersion(tpmStockRkApplyItem.getVersion()+1);
		this.update(tpmStockRkApplyItem);
		return true;
	}
}
